#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
简化的FastAPI服务启动脚本（无重载模式）
"""

import subprocess
import sys
import os

def install_requirements():
    """安装必要的依赖"""
    requirements = [
        "fastapi",
        "uvicorn[standard]",
        "pydantic",
    ]
    
    for package in requirements:
        try:
            __import__(package.split('[')[0])
            print(f"✓ {package} 已安装")
        except ImportError:
            print(f"正在安装 {package}...")
            subprocess.check_call([sys.executable, "-m", "pip", "install", package])

def main():
    """启动FastAPI服务"""
    print("规则制作器 FastAPI 服务启动器（生产模式）")
    print("=" * 60)
    
    # 检查并安装依赖
    install_requirements()
    
    # 启动服务
    print("\n启动服务...")
    print("服务地址: http://localhost:8000")
    print("API文档: http://localhost:8000/docs")
    print("ReDoc文档: http://localhost:8000/redoc")
    print("按 Ctrl+C 停止服务")
    print("=" * 60)
    
    try:
        import uvicorn
        from rule_maker_app import app
        
        # 生产模式启动（无重载）
        uvicorn.run(
            app, 
            host="0.0.0.0", 
            port=8000, 
            reload=False,  # 关闭重载模式
            log_level="info",
            access_log=True
        )
    except KeyboardInterrupt:
        print("\n服务已停止")
    except Exception as e:
        print(f"启动失败: {e}")
        print("请检查:")
        print("1. Oracle数据库连接是否正常")
        print("2. config.py文件是否存在并配置正确")
        print("3. common/rule_flatten_handle.py文件是否存在")

if __name__ == "__main__":
    main()